<?php

    namespace app\admin\controller;

    use think\Controller;
    use think\Db;
    use think\Session;
    class Index extends Controller
    {
        /**
         * 后台登录页面
         * @return \think\response\View
         */
        public function index()
        {
            return view('admin@index/login');
        }

        /**
         * 后台登录处理
         * @return \think\response\View
         */
        public function logindo()
        {

            //接收用户名和密码
            $username = input('post.username');
            $password = input('post.pwd');
          
    //        var_dump(md5(input('post.pwd')));exit;
            $data = Db::name('user')->where('username', $username)->where('userpass', md5($password))->find();
    // dump($data);die;
            if ($data) {
                    // var_dump($_SESSION);exit;
                   
                    // var_dump($list);exit;
                    // 把用户信息写入session
                    Session::set('admin_user',$data['username']);
                    

                     $list = Db::field('mname, aname')
                            ->name('user u')
                            ->join('user_role ur' , 'u.id = ur.uid')
                            ->join('role_node rn' , 'ur.rid = rn.rid')
                            ->join('node n', 'rn.nid = n.id')
                            ->where('u.id = '.$data['id'])
                            ->select();
                      
                    // var_dump($list);die;
                    //控制器名转换为大写
                    foreach ($list as $key => $val) {
                        $list[$key]['mname'] = ucfirst($val['mname']);
                    }
                    // dump($list);

                    $nodelist = array();
                   
                    //index的权限
                    $nodelist['Index'] = array('index');
                    $nodelist['Main'] = array('index');
                      // var_dump($nodelist);die;
                    //遍历重新拼装
                    foreach($list as $v){
                        $nodelist[$v['mname']][] = $v['aname'];
                        //把修改和执行修改 添加和执行添加 拼装到一起
                        if($v['aname']=="update"){
                            $nodelist[$v['mname']][]="doupdate";
                        }
                        if($v['aname']=="add"){
                            $nodelist[$v['mname']][]="doadd";
                        }
                    }
                    // var_dump($nodelist);die;
                    //将权限信息放置到session中
                    Session::set('nodelist', $nodelist);
                    return $this->success('登录成功','admin/Main/index');
               } else {
                return $this->error('登录失败');
             }
             // if ($data) {
             //            return $this->success('登录成功','admin/Main/index');
             //        } else {
             //            return $this->error('登录失败');
             //        }
            // var_dump($_SESSION['think']);die;
            // // 获取当前用户的角色信息
            // $user_role = Db::name('user_role')->where('uid', 'eq', $list['id'])->select();
            
          
            // $rid = array();
            // foreach ($user_role as $key => $value) {
            //     $rid[] = $value['rid'];
            // }

            // $nid = array();
            // // var_dump($rid);die;
            // foreach ($rid as $v){
                
            //      $a[]=  Db::name('role_node')->where('rid', 'eq', $v)->select();
             
            // }
            // var_dump($a);die;
            // dump($nid);die;

            //根据用户id获取对应的节点信息
            //$sql = "select n.mname,n.aname from lamp_user u join lamp_user_role ur on u.id=ur.uid join lamp_role_node rn on ur.rid=rn.rid join lamp_node n on rn.nid=n.id where u.id={$users['id']}";
            //$list = M()->query($sql);
            
          
        }

        /**
         * 后台登出处理
         * @return \think\response\View
         */
        public function logout()
        {
            $this->redirect('admin/index/index');
        }

    }
